<?php
/**
 * Created by PhpStorm.
 * User: zhaoyi
 * Date: 2022/1/26
 * Time: 11:46.
 */

declare(strict_types=1);

namespace App\Application\Api\Controller;

use App\Annotation\Api;
use App\Application\Admin\Model\Order;
use App\Application\Admin\Model\Package;
use App\Application\Admin\Model\Room;

use App\Application\Admin\Model\Roomlabel;
use App\Application\Admin\Model\Store;
use App\Application\Admin\Service\AdminSettingService;
use App\Application\Admin\Service\CityService;
use Hyperf\DbConnection\Db;
use Hyperf\Di\Annotation\Inject;
use Hyperf\HttpServer\Annotation\PostMapping;
use Hyperf\HttpServer\Annotation\GetMapping;
use Hyperf\HttpServer\Annotation\Controller;

#[Controller("/api/package")]
class PackageController extends AbstractController
{
    #[Inject]
    protected AdminSettingService $setting;

    #[Api]
    #[PostMapping("info")]
    function info()
    {
        $store_id = $this->request->input('store_id', '');
        $city_list = CityService::getInstance()
            ->getAllcity();
        $store = Room::where('store_id', $store_id)->select('*')
            ->first() ?: [];
        return compact('city_list', 'store');
    }

    /*
     *
     *门店包厢列表
     * */
    #[Api]
    #[PostMapping("PackageCourse")]
    function PackageCourse()
    {

        $add_time = $this->request->input('add_time', '');
        $room_id = $this->request->input('room_id', '');
        if (!empty($add_time)) {
            if ($add_time != "") {
                $add_times = date('Y-m-d ', strtotime($add_time));
            } else {
                $add_times = date('Y-m-d', time());
            }
        } else {
            $add_times = date('Y-m-d', time());
        }

        $package_id = $this->request->input('package_id', '');
        $where = [];
        $where[] = ['package_id', '=', $package_id];
        $lists = Package::where($where)
            ->select('*')
            ->get()->toArray();
        //保洁时长
        //已完成的订单才需要保洁0待开始，1保洁中，2已完成,3未开始
        $admintime = Store::where('store_id', $lists[0]['store_id'])->value('time');
        //订单 状态 订单状态0未开始，1进行中，2已结束，3已退款，4已取消，5已分账，6待付款
        $orders = Order::where('room_id', $room_id)->where('add_time', '>', time() - 10 * 3600)->whereIn('status', [0, 1, 6])
            ->select('*')
            ->orderBy('created_at', 'asc')
            ->get()->toArray();

        $ordersql = Order::where('room_id', $room_id)->where('add_time', '>', time() - 10 * 3600)->whereIn('status', [0, 1, 6])
            ->select('*')
            ->orderBy('created_at', 'asc')
            ->toRawSql();

        $da = [];
        $da['end_time'] = 0;
        foreach ($orders as $k => $v) {
            $add_hour = $v['add_time'];
            $end_hour = $v['end_time'] + $admintime * 60;
            if (self::is_time_cross($add_hour, $end_hour, time(), time() + $lists[0]['time'] * 3600)) {

                $da['end_time'] = $end_hour;
            }
        }

        $k = 0;
        //今天
        for ($i = 0; $i < 24; $i++) {
            $status = 0;
            $statusdata = array();
            $add_hour = strtotime($add_times) + $i * 3600;
            $end_hour = strtotime($add_times) + 3600 + $i * 3600;
            if ($add_hour < time()) {
                $status = 1;
            } else {
                if (count($orders) > 0) {
                    foreach ($orders as $m) {
                        $add_timex = $m['add_time'];
                        $end_timex = $m['end_time'] + $admintime * 60;

                        if ($m['add_time'] > time()) {
                            if ($this->is_time_cross($add_hour, $end_hour, $add_timex, $end_timex)) {
                                $statusdata[] = '1';
                            } else {
                                $statusdata[] = '0';
                            }
                        }
                        if ($m['add_time'] < time()) {
                            if ($this->is_time_cross($add_hour, $end_hour, $add_timex, $end_timex)) {
                                $statusdata[] = '1';
                            } else {
                                $statusdata[] = '0';
                            }
                        }
                    }
                    if (in_array('1', $statusdata, TRUE)) {
                        $status = 1;
                    } else {
                        $status = 0;
                    }
                }

            }
            $time[] = array(
                'add_time' => $add_hour,
                'end_time' => $end_hour,
                'name' => $k,
                'status' => $status,
            );
            $k += 1;
        }
        $k = 0;

        $add_times = date('Y-m-d', (strtotime($add_time) + 86400));

//明天
        for ($i = 0; $i < 24; $i++) {
            $status = 0;
            $statusdata = array();
            $add_hour = strtotime($add_times) + $i * 3600;
            $end_hour = strtotime($add_times) + 3600 + $i * 3600;
            if (count($orders) > 0) {
                foreach ($orders as $o => $info) {
                    $add_timex = $info['add_time'];
                    $end_timex = $info['end_time'] + $admintime * 60;
                    if ($info['add_time'] > time()) {
                        if ($this->is_time_cross($add_hour, $end_hour, $add_timex, $end_timex)) {

                            $statusdata[] = '1';
                        } else {
                            $statusdata[] = '0';
                        }
                    }
                    if ($info['add_time'] < time()) {
                        if ($this->is_time_cross($add_hour, $end_hour, $add_timex, $end_timex)) {

                            $statusdata[] = '1';
                        } else {
                            $statusdata[] = '0';
                        }
                    }
                }
                if (in_array('1', $statusdata, TRUE)) {
                    $status = 1;
                } else {
                    $status = 0;
                }

            }
            $time1[] = array(
                'add_time' => $add_hour,
                'end_time' => $end_hour,
                'name' => $k,
                'status' => $status,
            );
            $k += 1;
        }
        if ($da['end_time']) {
            $end_time = date('Y-m-d H:i:s', $da['end_time']);
        } else {
            $end_time = date('Y-m-d H:i', time());
        }

        return compact('lists', 'time', 'end_time', 'time1','orders','ordersql');

    }


}